package com.crm.system.controller.index;


import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.qo.index.NewsQO;
import com.crm.model.qo.index.NotifyQO;
import com.crm.model.vo.announcement.AnnouncementVO;
import com.crm.model.vo.announcement.NewsVO;
import com.crm.system.service.index.NewsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author WangFei
 * @date 2020/12/18 15:10
 */
@RestController
@RequestMapping("/NewsController")
public class NewsController {

    @Autowired
    NewsService newsService;

    /**
     * 获取轮播图片
     * @param request
     * @return
     */
    @GetMapping("/getImgList")
    public DataResponse getImgList(HttpServletRequest request){
        List<NewsVO> imgList = newsService.getImgList();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),imgList);
    }

    /**
     * 更新轮播图信息
     * @param request
     * @param newsQO
     * @return
     */
    @RequestMapping("/updateNewInfo")
    public DataResponse updateNewInfo(HttpServletRequest request, @RequestBody NewsQO newsQO){
        //先判断newsId是否为0，不为0就更新，为0就新增
        if(!StringUtils.isBlank(newsQO.getNewsId())){
            Boolean result = newsService.updateNewInfo(newsQO);
        }else {
            Boolean result = newsService.addNesInfo(newsQO);
        }
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 新闻列表查询
     * @param pageIndex
     * @param pageSize
     * @param searchInfo
     * @return
     */
    @GetMapping("/getNotifyList")
    public DataResponse getNotifyList(@RequestParam(value = "pageIndex") Integer pageIndex,
                                      @RequestParam(value = "pageSize") Integer pageSize,
                                      @RequestParam(value = "searchInfo", required = false)String searchInfo,
                                      @RequestParam(value = "publicStatus", required = false) Integer publicStatus,
                                      @RequestParam(value = "sort", required = false) String sort,
                                      @RequestParam(value = "sortOrder", required = false) String sortOrder){
        DhPageInfo<AnnouncementVO> notify = newsService.getNotifyList(pageIndex,pageSize,searchInfo,publicStatus,sort,sortOrder);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), notify);
    }

    /**
     * 新闻新增
     * @param notifyQO
     * @return
     */
    @PostMapping("/addNotify")
    public DataResponse addNotify(@RequestBody NotifyQO notifyQO){
        Boolean result = newsService.addNotify(notifyQO);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }


    /**
     * 新闻删除
     * @param id
     * @return
     */
    @GetMapping("/deleteNotify")
    public DataResponse deleteNotify(@Param(value = "id")String id){
        Boolean result =  newsService.deleteNotify(id);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 新闻修改
     * @param notifyQO
     * @return
     */
    @PostMapping("/updateNotify")
    public DataResponse updateNotify(@RequestBody NotifyQO notifyQO){
        Boolean result = newsService.updateNotify(notifyQO);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }



}
